package com.ztom.daily.n2022.m3;

/**
 * 如果相邻两个颜色均相同则删除当前颜色
 * <p>
 * https://leetcode-cn.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/
 *
 * @author ZhangTao
 */
public class Day22WinnerOfGame {

    public boolean winnerOfGame1(String colors) {
        if (colors == null || colors.length() == 0) {
            return false;
        }

        int n = 0;
        for (int i = 0; i < colors.length() - 2; i++) {
            String substring = colors.substring(i, i + 3);
            if ("AAA".equals(substring)) {
                n++;
            }
            if ("BBB".equals(substring)) {
                n--;
            }
        }
        return n > 0;
    }

    public boolean winnerOfGame(String colors) {
        if (colors == null || colors.length() == 0) {
            return false;
        }

        int n = 0;
        char[] chars = colors.toCharArray();
        for (int i = 1; i < chars.length - 1; i++) {
            if (chars[i - 1] == 'A'
                    && chars[i] == 'A'
                    && chars[i + 1] == 'A') {
                n++;
            }
            if (chars[i - 1] == 'B'
                    && chars[i] == 'B'
                    && chars[i + 1] == 'B') {
                n--;
            }
        }
        return n > 0;
    }
}
